#include <stdio.h>
#include <stdlib.h>
#include "Includes/Color.h"
#include "Includes/Matrix.h"
#include "Includes/Defines.h"
#include "Includes/Rectangle.h"
#include "Includes/Surface.h"
#include "Includes/Context.h"
#include "Includes/Driver.h"
#include <unistd.h>

/// <summary>
/// TODO
/// </summary>
ContextPtr GalCreateContext(void)
{
	ContextPtr ctx;
	if (!(ctx = (ContextPtr)malloc(sizeof(Context))))
		return NULL;

	ctx->clip.x = 0;
	ctx->clip.y = 0;
	ctx->clip.width = 0;
	ctx->clip.height = 0;
	ctx->comp_mode = 0;
	ctx->line_width = 1.0f;
	ctx->refcount = 1;
	ctx->buffer = NULL;
	GalMatrixIdentity(&ctx->projection);

	return ctx;
}

/// <summary>
/// TODO
/// </summary>
ContextPtr GalContextFromSurface(SurfacePtr surface)
{
	if (surface == NULL)
		return NULL;

	ContextPtr ctx;
	if (!(ctx = GalCreateContext()))
		return NULL;

	ctx->clip.x = 0;
	ctx->clip.y = 0;
	ctx->clip.width = surface->width;
	ctx->clip.height = surface->height;
	ctx->buffer = surface;
	ctx->line_width = 1.0f;
	ctx->comp_mode = 0;
	ctx->refcount = 1;

	return ctx;
}

/// <summary>
/// TODO
/// </summary>
int GalSetCompositingMode(ContextPtr ctx, int compositingMode)
{
	if (ctx == NULL)
		return -1;
	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalSetLineWidth(ContextPtr ctx, float width)
{
	if (ctx == NULL)
		return -1;
	ctx->line_width = width;
	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalSetClipping(ContextPtr ctx, RectanglePtr clip)
{
	if (ctx == NULL)
		return -1;
		
	if (clip == NULL)
		return -1;

	ctx->clip = *clip;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalContextClear(ContextPtr ctx, Color color)
{
	if (ctx == NULL)
		return -1;

	if (ctx->buffer == NULL)
		return -2;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalContextSwap(ContextPtr ctx)
{
	if (ctx == NULL)
		return -1;

	if (ctx->buffer == NULL)
		return -2;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int GalDestroyContext(ContextPtr ctx)
{
	if (ctx == NULL)
		return -1;

	free(ctx);
	return 0;
}
